﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.DAL
{
    public class BaseDAL<T> where T :class,new()
    {

        private DbContext dbContext = DbContextFactory.Create();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            dbContext.Set<T>().Add(t);
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="list"></param>
        public void Add(List<T> list)
        {
            dbContext.Set<T>().AddRange(list);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        public void Delete(T t)
        {
            dbContext.Set<T>().Remove(t);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        public void Delete(List<T> list)
        {
            dbContext.Set<T>().RemoveRange(list);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        public void Update(T t)
        {
            dbContext.Set<T>().AddOrUpdate(t);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Query()
        {
            return dbContext.Set<T>().AsQueryable();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            var res = dbContext.SaveChanges();
            return res > 0;
        }



    }
}
